/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package trivia.gui;

import javax.microedition.lcdui.*;
import trivia.Subject;
import trivia.Problem;

/**
 *
 * @author mourawi
 */
public class ProblemForm extends Form {

    private Subject subject;

    private StringItem question;

    private ChoiceGroup answers;
    
    public ProblemForm() {
        super("Subject");
        subject = null;
        question = new StringItem("", "", Item.PLAIN);
        answers = new ChoiceGroup("", Choice.EXCLUSIVE);
        answers.setLayout(ImageItem.LAYOUT_DEFAULT);
        answers.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
        answers.setSelectedFlags(new boolean[] {  });
        append(question);
        append(answers);
        addCommand(new Command("Cancel", Command.CANCEL, 0));
        addCommand(new Command("Answer", Command.OK, 0));
    }

    public int getUserAnswer() {
        return answers.getSelectedIndex();
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
        setTitle(subject.getName());
    }

    public void displayProblem(int problemIndex, Problem problem) {
        setTitle(subject.getName() + " " + (problemIndex+1) + "/" + subject.getProblemsNumber());
        question.setText(problem.getQuestion());
        answers.deleteAll();
        int max = problem.getAnswersNumber();
        for(int i=0; i<max; i++) {
            answers.append(problem.getAnswer(i), null);
        }
    }
}
